// pages/packageA/commentList/commentList.js
import Dialog from '@vant/weapp/dialog/dialog';
var util = require('../../../utils/util')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    isIphoneX: app.globalData.isIphoneX,
    pageNum: 1,
    sortedType: "1", //1-热门 2-最新
    goodsId: "",
    name: "",
    picUrl: "",
    commentList: [],
    show: false,
    checkedData: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.info('==== options ====', options)
    this.setData({
      goodsId: options.goodsId || '1181002',
      name: options.name || '奢华植鞣头层皮',
      picUrl: options.picUrl || 'https://hongyueshop.oss-cn-beijing.aliyuncs.com/0b5a120230403205935.png'
    })
    this.getList()
  },

  tabClick(e) {
    this.setData({
      commentList: [],
      pageNum: 1,
      sortedType: e.currentTarget.dataset.flag
    })
    this.getList()
  },

  getList() {
    wx.get(`${wx.api.goodsTrends}?goodsId=${this.data.goodsId}&sortedType=${this.data.sortedType}&pageNum=${this.data.pageNum}`, res => {
      console.log('==== goodsTrends ===', res)
      if (res.code == 200) {
        if (res.rows.length > 0) {
          this.setData({
            commentList: this.data.commentList.concat(res.rows)
          })
        } else {
          if (this.data.pageNum > 1) {
            this.setData({
              noList: true
            })
          }
        }

      }
    }, fail => {})
  },

  // 刷新页面数据
  refreshList() {
    this.setData({
      commentList: [],
      pageNum: 1,
      noList: false
    })
    this.getList()
  },

  agree: util.throttle(function (e) {
    let {
      isagree,
      id,
      index
    } = e.currentTarget.dataset
    let api = wx.api.trendAgree
    if (isagree == 1) api = wx.api.trenDdeleteAgree
    wx.post(`${api}${id}`, {}, res => {
      console.log('==== agree ===', res)
      if (this.data.commentList[index].isAgree == 1) {
        this.setData({
          [`commentList[${index}].isAgree`]: 0,
          [`commentList[${index}].star`]: this.data.commentList[index].star - 1
        })
      } else {
        this.setData({
          [`commentList[${index}].isAgree`]: 1,
          [`commentList[${index}].star`]: this.data.commentList[index].star + 1
        })
      }
      wx.showToast({
        title: this.data.commentList[index].isAgree == 1 ? '谢谢支持' : '已取消',
        icon: 'none',
        duration: 1000
      })
    }, fail => {})
  }, 3000),

  toZhao(e) {
    let urls = e.currentTarget.dataset.urls
    wx.previewImage({
      showmenu: false,
      urls: urls // 需要预览的图片http链接列表
    })
  },

  showPopup(e) {
    this.setData({
      show: true,
      checkedData: {
        ...e.currentTarget.dataset.item,
        index: e.currentTarget.dataset.index
      }
    });
  },

  onClose() {
    this.setData({
      show: false
    });
  },

  // 复制文案
  copyText(e) {
    let that = this
    wx.setClipboardData({
      data: this.data.checkedData.content,
      success(ress) {
        wx.getClipboardData({
          success(res) {
            that.onClose()
            console.log(res.data) // data
          }
        })
      }
    })
  },

  delete() {
    Dialog.confirm({
      title: '温馨提示',
      message: '删除评论后，评论下所有的回复都会被删除',
      confirmButtonText: '删除评论',
      cancelButtonText: '取消'
    }).then(() => {
      let {
        id,
        index
      } = this.data.checkedData
      wx.post(wx.api.trendDel + id, {}, res => {
        Dialog.alert({
          title: '温馨提示',
          message: '已删除',
          confirmButtonText: '好的'
        })
        this.onClose()
        this.setData({
          commentList: this.data.commentList.filter((item, ind) => ind != index)
        })
      }, fail => {})
    })
  },

  // 发表评论
  toComment() {
    wx.navigateTo({
      url: `/pages/packageA/comment/comment?goodsId=${this.data.goodsId}`
    })
  },

  // 回复
  toComment1() {
    let checkedData = this.data.checkedData
    wx.navigateTo({
      url: `/pages/packageA/comment/comment?id=${checkedData.id}&nickName=${checkedData.nickName}&userId=${checkedData.userId}`
    })
    this.onClose()
  },

  toComment2(e) {
    let {
      trendsId,
      fromNickName,
      userId
    } = e.currentTarget.dataset.item
    wx.navigateTo({
      url: `/pages/packageA/comment/comment?id=${trendsId}&nickName=${fromNickName}&userId=${userId}`
    })
  },

  toCommentDetail(e) {
    let {
      id,
      index
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/packageA/commentDetail/commentDetail?id=${id}&name=${this.data.name}&picUrl=${this.data.picUrl}&index=${index}`
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      userInfo: app.globalData.userInfo || wx.getStorageSync('userInfo'),
    })
    let that = this
    wx.getStorage({ // 详情页更新数据
      key: "commentUpdate",
      success(res) {
        console.info('==== commentUpdate ====', res)
        if (res.data) {
          let {
            index,
            isAgree,
            star
          } = res.data
          that.setData({
            [`commentList[${index}].isAgree`]: isAgree,
            [`commentList[${index}].star`]: star
          })
          wx.removeStorage({
            key: 'commentUpdate',
          })
        }
      }
    })
    wx.getStorage({ // 详情页更新数据
      key: "commentDelete",
      success(res) {
        console.info('==== commentDelete ====', res)
        if (res.data) {
          let {
            index
          } = res.data
          that.setData({
            commentList: that.data.commentList.filter((item, ind) => ind != index)
          })
          wx.removeStorage({
            key: 'commentDelete',
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.noList) return
    this.data.pageNum++
    this.getList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})